package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.dao.TeacherDao;
import cn.edu.sdjzu.xg.bysj.domain.*;
import cn.edu.sdjzu.xg.bysj.domain.authority.Actor;
import lombok.extern.slf4j.Slf4j;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@Slf4j
public final class TeacherService {
	private static final TeacherDao teacherDao = TeacherDao.getInstance();
	private static final TeacherService teacherService = new TeacherService();

	private TeacherService() {
	}

	public static TeacherService getInstance() {
		return teacherService;
	}

	//无条件查询全部方法
	public Collection<Teacher> findAll() throws SQLException {
		return this.findAll(null, null);
	}
	//条件查询加分页查询的查询全部
	public Collection<Teacher> findAll(Pagination pagination
			, List<Condition> conditionList_json_str) throws SQLException {
		//获取连接
		Connection conn = JdbcHelper.getConn();
		Collection<Teacher> teachers=
				teacherDao.findAll(pagination,conditionList_json_str,conn);
			//关闭连接
			JdbcHelper.close(conn);
		return teachers;
	}
	//根据id查询
	public Teacher find(Integer id) throws SQLException {
		//获取连接
		Connection conn = JdbcHelper.getConn();
		Teacher teacher = teacherDao.find(id,conn);
		//关闭连接
		JdbcHelper.close(conn);
		return teacher;
	}
	//修改方法
	public boolean update(Teacher teacher) throws SQLException {
		//获取连接
		Connection conn = JdbcHelper.getConn();
		//将自动提交设置为false
		conn.setAutoCommit(false);
		boolean updated = false;
		try {
			//执行修改方法
			updated = teacherDao.update(teacher,conn);
			//提交事务
			conn.commit();
		}catch (SQLException e){
			//回滚到事务开始
			conn.rollback();
			//打印异常
			log.trace(e.toString());
		}finally {
			//将自动提交设置为true
			conn.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(conn);
		}
		return updated;
	}
	//删除方法
	public boolean delete(Integer id) throws SQLException {
		//如果该教师有关联的课题，不能删除
		//获取连接
		Connection conn = JdbcHelper.getConn();
		//将自动提交设置为false
		conn.setAutoCommit(false);
		boolean deleted = false;
		try {
			//执行删除方法
			deleted = teacherDao.delete(id,conn);
			//提交事务
			conn.commit();
		}catch (SQLException e){
			//回滚到事务开始
			conn.rollback();
			//打印异常
			log.trace(e.toString());
		}finally {
			//将自动提交设置为true
			conn.setAutoCommit(true);
			//关闭连接
			JdbcHelper.close(conn);
		}
		return deleted;
	}

	/**
	 * 添加Teacher对象
	 * @param teacher 要添加的Teacher对象
	 * @return 添加成功返回true，否则false
	 * @throws SQLException
	 */
	public Boolean add(Teacher teacher) throws SQLException {
		//获得进行事务的连接，添加教师和添加用户都使用本连接对象
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		Boolean added = false;
		try {
			teacher.setId(ActorService.getInstance().add());
			//创建用户对象，密码和用户名取自teacher字段值no
			User user = new User(teacher.getNo(), teacher.getNo(), null, teacher);
			Integer id = UserService.getInstance().add(user);
			//为student的id赋值
			teacher.setId(id);

			added=teacherDao.add(teacher, connection);
			//提交事务
			connection.commit();
		} catch (SQLException e) {
			//回滚事务中所有操作
			e.printStackTrace();
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(null, connection);
		}
		return added;
	}
	public int countAll(Connection connection,Department department)throws SQLException{
		connection.setAutoCommit(false);
		int count = 0;
		try {
			count = teacherDao.countAll(department,connection);
		}catch (SQLException e){
			connection.rollback();
		}finally {
			connection.setAutoCommit(true);
		}
		return count;
	}

}
